package org.zhxy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;
import org.zhxy.model.entity.Admin;
import org.zhxy.model.entity.Student;
import org.zhxy.service.StudentService;
import org.zhxy.mapper.StudentMapper;
import org.springframework.stereotype.Service;

/**
 * @author walter
 * @description 针对表【tb_student】的数据库操作Service实现
 * @createDate 2025-07-01 14:17:33
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student>
        implements StudentService {

    // 根据姓名查询学生信息
    @Override
    public Student findByName(String name) {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Student::getName, name);
        return getOne(wrapper);
    }

    // 分页条件查询
    @Override
    public IPage<Student> page(IPage<Student> page, Student student) {
        LambdaQueryWrapper<Student> wrapper = new LambdaQueryWrapper<>();
        String clazzName = student.getClazzName();
        wrapper.eq(StringUtils.hasText(clazzName), Student::getClazzName, clazzName);
        String name = student.getName();
        wrapper.like(StringUtils.hasText(name), Student::getName, name);

        return page(page, wrapper);
    }

    @Override
    public boolean updatePwd(Integer id, String newPwd) {
        LambdaUpdateWrapper<Student> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Student::getId, id);
        wrapper.set(Student::getPassword, DigestUtils.md5DigestAsHex(newPwd.getBytes()));
        return update(wrapper);
    }
}




